package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.display.newAttack.AttackLogic;
	import engine.display.newAttack.vo.DropVo;
	import engine.display.newAttack.vo.PrizeVo;
	import engine.event.SoundEvent;
	import engine.flag.ResourcesPath;
	import engine.net.messages.CustomToClientFlag;
	import engine.tip.TipsMainLogic;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.group.NxViewGroup;

	/**
	 * 普通战斗结算L 
	 * @author liuel
	 * 
	 */
	public class ActionResultLogic
	{
		public static var _self :ActionResultLogic;
		private var _ui:ActionResultUI;
		private var _model:ModelLocator;
		/**队伍数量**/
		private var _teamCount              :int=1;
		/**掉落物品数量**/
        private var _itemCount               :int=0;
		/**掉落物品数组**/
		private var	_itemArr              :Vector.<DropVo>;
		/**队伍信息数组**/
		private var    _teamArr              :Vector.<PrizeVo> ;
		/**保存经验值控件**/
		private var _txtArr:Array
		private var _imgDefaultWidth		 :uint		= 50;
		
		private var _imgDefaultHeight		 :uint		= 50;
		/**物品格子对大数目**/
		private var _itemgroupMax            :uint ;
		/**队伍格子对大数目**/
		private var _teamgroupMax            :uint ;
		private var _time                    :Timer;

		private var _itemMC		              :MovieClip;
		private var _img		:NxClip;
		
		/**离开函数**/
		private var _leftFun                :Function =null;
		/**重新播放函数**/
		private var _againFun  :Function =null;
		public static function getInstance():ActionResultLogic
		{
			if( _self == null)
			{
				_self = new ActionResultLogic();
			}
			return _self;
		}
		public function ActionResultLogic()
		{
		}
		public function  Init():void
		{
			_model = ModelLocator.getInstance();
			
			initInfo();
			//战斗结束响应函数
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SHOW_FIGHT_INFO),
				onDropItem);
			
		}
		private function initInfo():void
		{
			_ui            = ActionResultUI.getInstance();	

			_itemgroupMax  = _ui.bootyGroup.col;
			_teamgroupMax  = _ui.teamGroup.row*_ui.teamGroup.col
				
			initGroup(_ui.teamGroup,_teamgroupMax);
			initGroup(_ui.bootyGroup,_itemgroupMax);
			
			_time=new Timer(40,20);
		}
		public function	BeforeShow():void
		{
//			_ui.label.htmlStr = StaticIni.getSwfLangStr("UIString_1000040");
			_ui.two.visible=false;
			_ui.three.visible=false;
			var lv:int = int(StaticIni.getIniVar("ActionEctypeResult_Guide","showLvevl"));
			if(_model.scene.Role.getPlayerLevel()<=lv)
			{
				_ui.guideMc.visible = true;
				_ui.guideMc.play();
			}
			else
			{
				_ui.guideMc.visible = false;
				_ui.guideMc.stop();
			}
			_ui.guideMc.mouseChildren = _ui.guideMc.mouseEnabled = false;
			addListener();
			_model.dispatchEvent(new SoundEvent(SoundEvent.BATTLE_SUM));
		}
		public function	AfterHide():void
		{
			removeListener();	
		}
	
		private function addListener():void
		{
			_ui.addEventListener(MouseEvent.CLICK,   onClick);
			_time.addEventListener(TimerEvent.TIMER, addExperience);
			_ui.bootyGroup.addItemsEvent(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.bootyGroup.addItemsEvent(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.one.addEventListener(MouseEvent.MOUSE_OVER,onshowTip);
			_ui.two.addEventListener(MouseEvent.MOUSE_OVER,onshowTip);
			_ui.three.addEventListener(MouseEvent.MOUSE_OVER,onshowTip);
			_ui.one.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.two.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.three.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);

		}
		private function removeListener():void
		{
			_ui.removeEventListener(MouseEvent.CLICK, onClick);
			_ui.bootyGroup.removeItemsEvent(MouseEvent.MOUSE_OVER,onShowTips);
			_ui.bootyGroup.removeItemsEvent(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.one.removeEventListener(MouseEvent.MOUSE_OVER,onshowTip);
			_ui.two.removeEventListener(MouseEvent.MOUSE_OVER,onshowTip);
			_ui.three.removeEventListener(MouseEvent.MOUSE_OVER,onshowTip);
			_ui.one.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.two.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
			_ui.three.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
		}
		private function onshowTip(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case _ui.one:
					TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIString_1000041"));
					break;
				case _ui.two:
					TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIString_1000042"));
					break;
				case _ui.three:
					TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIString_1000043"));
					break;
			}
		}
		private function initGroup(group :NxViewGroup,num:int):void
		{
		 
		    var txt1		:TextField;
			var txt2        :TextField;
			var PlyType		:NxClip
			for(var i : int = 0 ; i < num ; i ++)
			{
				_img				=	new NxClip(); 
				_itemMC =	group.getItemByIndex(i);
				_img.defaultImage=	null;
				_img.isCache		=	true;
				_itemMC["infoTxt4"].text="";
				if(group ==_ui.bootyGroup)
				{
					_img.width	=	 _img.height	=	40;
					_img.x =_itemMC["img2"].x-1;
					_img.y =_itemMC["img2"].y;
					//战利品
					txt1=_itemMC["titleTxt2"];   //第一行玩家名字
					txt1.text=""
					txt1.wordWrap=false;
					txt1.autoSize = txt1.autoSize = TextFieldAutoSize.LEFT;
					txt1.height = 24;
					_itemMC.mouseChildren = false;
				}
				else
				{
					_img.width	=	 _img.height	=	50;
					_img.x =_itemMC["img2"].x+1;
					_img.y =_itemMC["img2"].y+2;
					//队伍
					txt1=_itemMC["titleTxt2"];   //第一行玩家名字
					txt1.text=""
					txt1.autoSize = txt1.autoSize = TextFieldAutoSize.LEFT;
					txt1.height = 24;
					_itemMC.mouseChildren = false;
					
					txt2=_itemMC["infoTxt5"];
					txt2.text="";
					txt2.autoSize = txt2.autoSize = TextFieldAutoSize.LEFT;
					txt2.height = 17;
					_itemMC["infoTxt5"].width = 60;
					_itemMC.mouseChildren = false;
				}
				_img.scaleContent	=	true;
//				_img.maintainAspectRatio	=	false;
			    _itemMC["img1"] = _img ;
				_itemMC.addChild(_img);
			
			}
		}

		/**
		 * 更新一个玩家信息 
		 * @param index 人物所在数组索引
		 * @param item
		 * 
		 */		
		private function updataTeamGroup(index : int, arr : Vector.<PrizeVo>):void
		{
		
			var itemMC	 	: MovieClip	=	_ui.teamGroup.getItemByIndex(index);
			if(itemMC != null)
			{	
				if(index>=_teamCount)
				{
					itemMC.visible=false;
					return ;
				}
				itemMC.visible=true;	
				var clothModel:int=0;
				if(arr[index].type == 2)//主角
				{
					clothModel=_model.scene.Role.clotheModel;
					(itemMC["img1"] as NxClip).source   =  ResourcesPath.getMiniHeadPic(int(arr[index].model),clothModel);
					itemMC["titleTxt3"].htmlText  =StaticIni.getSwfLangStr("CapitalType_1")+"+"+AttackLogic.getInstance().Experience;
				}
				else
				{
					(itemMC["img1"] as NxClip).source   =  ResourcesPath.getMiniHeadPic(int(arr[index].model),"");
					itemMC["titleTxt3"].htmlText  ="";
				}
				
				itemMC["titleTxt2"].htmlText       =  arr[index]["playerName"]+""; 
				itemMC["infoTxt4"].htmlText=StaticIni.getSwfLangStr("UIStr_1001157");
				itemMC["infoTxt5"] .text= "0";
				_txtArr.push(itemMC["infoTxt5"])
			}
		}
		/**
		 * 更新队伍信息 
		 * @param index 人物所在数组索引
		 * @param item
		 * 
		 */		
		private function updataGroup(arr:Vector.<PrizeVo>):void
		{
			_txtArr=[];
			arr = arr.sort(sortback)
			for(var i : int = 0; i <_teamgroupMax; i ++)
			{
				updataTeamGroup(i, arr);
			}
		}
		private function sortback(vo1:PrizeVo,vo2:PrizeVo):int
		{
			return vo1.type-vo2.type
		}
		/**
		 * 清除一个玩家信息 
		 * @param index
		 * 
		 */		
		private function removeTeamGroup(index : int):void
		{
			var itemMC : MovieClip = _ui.teamGroup.getItemByIndex(index);
			if(itemMC != null)
			{
				(itemMC["img"] as NxClip).source = "";
				(itemMC["img"] as NxClip).clear();
				 itemMC["titleTxt2"].text ="";
				 itemMC["infoTxt5"] .htmlText="";
				 itemMC["infoTxt4"].text="";
			}
		}
		/**
		 * 清除所有队伍信息
		 * @param group
		 * 
		 */		
		private function clearTeamGroup():void
		{
			for(var i : int = 0; i < _teamCount; i ++)
			{
				removeTeamGroup(i);
			}
		}
		
		/**
		 * 更新一个物品信息 
		 * @param index 物品所在数组索引
		 * @param item
		 * 
		 */		
		private function updataItemGroup(index : int, arr : Vector.<DropVo>):void
		{
			
			var  itemMC	:MovieClip	=	_ui.bootyGroup.getItemByIndex(index);
			if(itemMC != null)
			{	
				if(index>=_itemCount)
				{
					itemMC.visible=false;
					return;
				}
				itemMC.visible=true;
				(itemMC["img1"] as NxClip).source   = ResourcesPath.getPropsURL( int(arr[index].model));
				itemMC["titleTxt2"].htmlText   =arr[index].name;
				itemMC["infoTxt4"].text = "X"+arr[index].amount;
				itemMC["date"] = arr[index].config;
			}	
		}
		/**
		 * 更新物品信息 
		 * @param index 物品所在数组索引
		 * @param item
		 * 
		 */		
		private function updataItem(arr:Vector.<DropVo>):void
		{
		  
			for(var i : int = 0; i < _itemgroupMax; i ++)
			{
				updataItemGroup(i, arr);
			}
			_model.dispatchEvent(new SoundEvent(SoundEvent.ROUND_NUM));

		}
		
		/**
		 * 清除一个物品信息 
		 * @param index
		 * 
		 */		
		private function removeItemGroup(index : int):void
		{
			var itemMC : MovieClip = _ui.bootyGroup.getItemByIndex(index);
			if(itemMC != null)
			{
				(itemMC["img1"] as NxClip).source = "";
				(itemMC["img1"] as NxClip).clear();
				itemMC["titleTxt2"].text ="";
				itemMC["infoTxt4"].text ="";
				itemMC["date"]= null;
			}
		}
		/**
		 * 清除所有掉落物品信息
		 * @param group
		 * 
		 */		
		private function clearItemGroup():void
		{
			for(var i : int = 0; i < _teamCount; i ++)
			{
				removeItemGroup(i);
			}
		}
		/**
		 *添加蒙板 
		 * 
		 */		
        private function addPanle():void
		{
			_ui.graphics.beginFill(0xfff111,0.0);
			var p:Point=new Point(0,0);
			p=_ui.globalToLocal(p);
		    _ui.graphics.drawRect(p.x,p.y,StageInfo.stage.stageWidth,StageInfo.stage.stageHeight);
			_ui.graphics.endFill();
		}
		///////////////////////////////////////////////监听函数////////////////////////////////////////////////

		private function addExperience(e:TimerEvent):void
		{

			for each (var txt:TextField in _txtArr) 
			{
				var sI:int=int(txt.text);
				var index:int=_txtArr.indexOf(txt);
				var targetEx:int=_teamArr[index]["exp"];
				var sp:Number=targetEx/_time.repeatCount;
				if(sp<1)
				{
					sp =1;
				}
				if(_time.currentCount==_time.repeatCount)
				{
					txt.text="+"+targetEx+"";
					
				}
					
				else
				{
					if(int(sI+sp)==targetEx)
					{
						txt.text="+"+targetEx;
						_time.reset();
					}
					txt.text="+"+int(sI+sp)+"";
				}
					
			}
			if(_time.currentCount == _time.repeatCount)
			{
				_time.removeEventListener(TimerEvent.TIMER,addExperience);
				_time.reset();
			}
		}

		/**
		 * 拾取物品
		 * @param e
		 * 
		 */		
		private function onClick(e:MouseEvent):void
		{
					
			switch(e.target)
			{
				case _ui.one:
					onClickOne();
					break;
				case _ui.two:
					onClickTwo();
					break;
				case _ui.three:
					onClickThree();
					break;
				case _ui.labelBtn:
					onPick();
					break;
			}
			_ui.graphics.clear();
		}
		private function onPick():void
		{
            _leftFun();
     		_ui.hide();
		}
		/**
		 * 点击重新播放
		 * @param e
		 * 
		 */		
		private function onClickOne():void
		{
			_againFun();
			_ui.hide();
		}
		/**
		 * 发送到聊天窗
		 * @param e
		 * 
		 */		
		private function onClickTwo():void
		{
			
		}
		/**
		 * 复制录像地址 
		 * @param e
		 * 
		 */		
		private function onClickThree():void
		{
			
		}
		private function onShowTips(e:MouseEvent):void
		{
			var mc:MovieClip =e.currentTarget as MovieClip;
			if(mc == null)
				return;
			var obj:Object =TipsMainLogic.instance.getStaticObjWithID(mc.date);
			if(obj == null)
				return;
			TipsMainLogic.instance.showMain(obj);
		}
		private function onHideTips(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		/**
		 * 接受队伍信息 
		 * @param arr 队伍数组
		 * 
		 */		
		private function onAcceptTeam(arr:Vector.<PrizeVo>):void
		{
			_teamArr=arr;
			taxisFunc(_teamArr);
			_teamCount= _teamArr.length;
			if(_ui.created ==false)
			{
				_ui.showOrHide();
//				addPanle();
			}
			updataGroup(_teamArr);
			starAnimation();
		}
		private function taxisFunc(arr:Vector.<PrizeVo>):void
		{
			var arr1:Vector.<PrizeVo> = null;
			arr = arr.sort(byStateFunc);
		}
		private function byStateFunc(vo1:PrizeVo,vo2:PrizeVo):int
		{
			if(vo1.playerName == _model.scene.Role.getPlayerName())
			{
				return -1;
			}
			else
			{
				return 1;
			}
			
		}
		/**
		 *动画开始 
		 * 
		 */		
		private function starAnimation():void
		{
			_time.start();
			
		}
		/**
		 * 接受掉落物品信息
		 * @param e
		 * 
		 */		
		private function  onDropItem(arr:Vector.<DropVo>):void
		{
			_itemCount=arr.length;
			if(_itemCount>0)
			{
				_ui.bootyGroup.visible=true;
				_ui.botypic.visible=false;
				_itemArr=arr;
				updataItem(_itemArr);
				
			}
			else
			{
				_ui.bootyGroup.visible=false;
				_ui.botypic.visible=true;
			}
			
			if(_ui.created ==false)
			{
				_ui.showOrHide();
			}
		}
		
		public function setFunc( leaveFunc : Function, againFightFunc : Function ):void
		{
			_leftFun =  leaveFunc;
			_againFun = againFightFunc;
		}
		
		
		/**
		 *	战斗评分 
		 * @param itemInfos				掉落数组
		 * @param teamInfos				队伍信息
		 * 
		 */
		public function resultInfo( itemInfos : Vector.<DropVo>, teamInfos : Vector.<PrizeVo> ):void
		{
			onAcceptTeam(teamInfos);
			
			onDropItem(itemInfos);
		}
	}
}